package list;

import com.oracle.webservices.internal.api.databinding.DatabindingMode;

/**
 * @version 1.0
 * 功能目的描述 ：用于    自己创建一个单链表     ========>>>>>>>>>>>
 * @author： pang-yun
 * @date： 2021-07-20 10:22
 */

public class SingleLinkedListByPang {
    public static void main(String[] args) {

        Node node1 = new Node(1, "及时雨", "宋江");
        Node node2 = new Node(2, "行者", "武松");
        Node node3 = new Node(3, "神行太保", "戴宗");
        Node node4 = new Node(4, "金毛犬", "段景住");

        SingleLinkedList singleLinkedList = new SingleLinkedList();
        singleLinkedList.addByOrder(node4);
        singleLinkedList.addByOrder(node3);
        singleLinkedList.addByOrder(node1);
        singleLinkedList.addByOrder(node2);
        singleLinkedList.list();


        System.out.println("----------------------------------");
        Node node5 = new Node(4, "怒王", "秦仲海");
        singleLinkedList.update(node5);
        singleLinkedList.list();

        System.out.println("---------delete--------------");
        singleLinkedList.delete(node5);
        singleLinkedList.list();
    }


}

//节点
class Node {
    public Integer number;
    public String name;
    public String nickName;
    public Node next;

    public Node(Integer number, String name, String nickName) {
        this.number = number;
        this.name = name;
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        return "SingleLinkedList{" +
                "number=" + number +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }

}

//单链表
class SingleLinkedList {
    private Node head = new Node(0, "head", "头节点");

    //直接在最后插入
    public void add(Node node) {

        Node temp = head;
        while (true) {
            if (head.next == null) {
                break;
            }
            temp = temp.next;
        }
        temp.next = node;
    }


    //按照number 顺序插入
    public void addByOrder(Node node) {
        Node temp = head;
        boolean flag = false;

        while (true) {
            if (head.next == null) {
                break;
            }
            if (temp.next.number > node.number) {
                break;
            }
            if (temp.number == node.number) {
                flag = true;
                break;
            }
            temp = temp.next;
        }

        if (flag) {
            System.out.printf("%d已经存在\n", node.number);
        } else {
            node.next = temp.next;
            temp.next = node;

        }

    }


    //遍历
    public void list() {

        if (head.next == null) {
            System.out.println("list为空");
            return;
        }
        Node temp = head;
        while (true) {
            if (temp.next == null) {
                System.out.println(temp.toString());
                break;
            } else {
                System.out.println(temp.toString());
                temp = temp.next;
            }

        }

    }

    //更新
    public void update(Node node) {
        Node temp = head;
        if (temp.next == null) {
            System.out.println("list为空");
            return;
        }

        boolean flag = false;
        while (true) {
            if (temp.number == node.number) {

                flag=true;
                break;
            }
            if (temp.next == null) {
                break;
            }

            temp=temp.next;
        }

        if (flag){
            temp.name=node.name;
            temp.nickName = node.nickName;
        }else {
            System.out.println("没有对应的node");
        }
    }


    //删除
    public void delete(Node node){
        Node temp = head;
        if (temp.next==null){
            System.out.println("list为空");
        }

        boolean flag = false;

        while (true){
            if (temp.next.number==node.number){
                flag =true;
                break;
            }
            if (temp.next==null){
                break;
            }
            temp=temp.next;
        }
        if (flag){
            temp.next = temp.next.next;
        }else {
            System.out.println("无相关node");
        }
    }
}
